﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using MyBet.Web;
using ParieurPro.DAL;

namespace MyBet.Web.admin
{
    public partial class UpdateResults : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
                try
                {
                    DateTime startDate = DateTime.UtcNow;
                    String outputPath = Server.MapPath(@"~\import");
                    if (FileUpload1.FileName.ToLower().EndsWith(".xlsx"))
                    {
                        String fullPath = System.IO.Path.Combine(outputPath, FileUpload1.FileName);
                        if (File.Exists(fullPath))
                        {
                            File.Delete(fullPath);
                        }
                        FileUpload1.SaveAs(fullPath);
                        UpdateScores scores = new UpdateScores();
                        scores.UpdateFromFile(fullPath);

                        ImportResultLabel.Text = "File " +
                        FileUpload1.PostedFile.FileName + " uploaded<br>" +
                        FileUpload1.PostedFile.ContentLength + " kb<br>" +
                         "Content type: " +
                         FileUpload1.PostedFile.ContentType;

                    }
                    else
                    {
                        ImportResultLabel.Text = "ERROR: Bad extension";
                    }

                    TimeSpan elapsedTime = DateTime.UtcNow-startDate;
                    ImportResultLabel.Text += String.Format("<br>Elapsed times :{0}", elapsedTime.ToString());
                }
                catch (Exception ex)
                {
                    ImportResultLabel.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                ImportResultLabel.Text = "You have not specified a file.";
            }



        }

    }
}